package org.jeecg.common.datasource.jdbc.vo;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(
    value = "ResponseData",
    description = "接口消息"
)
public class ResponseData<T> {

    @ApiModelProperty("是否执行成功")
    private Boolean success;

    @ApiModelProperty("页面编码，200为正常")
    private Integer code;

    @ApiModelProperty("响应数据")
    private T result;

    @ApiModelProperty("错误信息")
    private String message;

    @ApiModelProperty("其他信息")
    private JSONObject other = new JSONObject();

    public ResponseData() {
        this(null);
    }

    public ResponseData(T result) {
        this.code = 200;
        this.result = result;
        this.message = "";
        this.success = true;
    }

    public ResponseData(Integer code, String detailMsg) {
        this.code = code;
        this.message = detailMsg;
        this.success = (this.code == 200);
    }

    public static <T> ResponseData<T> success(T result){
        return new ResponseData<>(result);
    }

    public static <T> ResponseData<T> error(String detailMsg){
        return new ResponseData<>(501, detailMsg);
    }

    public static <T> ResponseData<T> error(Integer code, String detailMsg){
        return new ResponseData<>(code, detailMsg);
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
        this.success = (this.code == 200);
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public Boolean getSuccess() {
        return success;
    }

    public JSONObject getOther() {
        return other;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ResponseData<T> addOther(String key, Object value) {
        this.other.put(key, value);
        return this;
    }

    public ResponseData<T> setOther(JSONObject other) {
        this.other = other;
        return this;
    }
}
